package com.boot2523.myrabbitmq.q5;

import com.rabbitmq.client.Channel;
import com.rabbitmq.client.Connection;
import com.rabbitmq.client.ConnectionFactory;

public class Producer {
    private static final String EXCHANGE_NAME = "topic_logs";

    public static void main(String[] args) throws Exception {
        ConnectionFactory factory = new ConnectionFactory();
        factory.setHost("localhost");

        try (Connection connection = factory.newConnection();
             Channel channel = connection.createChannel()) {

            // 声明一个 topic 类型的 exchange
            channel.exchangeDeclare(EXCHANGE_NAME, "topic");

            // 发送不同类型的消息
            sendMessage(channel, "土豆 0.5元", "食物.1");
            sendMessage(channel, "牛肉  20元", "食物.25435");
            sendMessage(channel, "大连  30度", "135435345.天气");
            sendMessage(channel, "成都  32度", "2无可奈何花落去.天气");
            sendMessage(channel, "中国石油  8.89", "stock.14543534534");
            sendMessage(channel, "工商银行  7.86", "stock.2");
        }
    }

    private static void sendMessage(Channel channel, String message, String routingKey) throws Exception {
        channel.basicPublish(EXCHANGE_NAME, routingKey, null, message.getBytes("UTF-8"));
        System.out.println(" [x] Sent '" + message + "' with routing key: " + routingKey);
    }
}
